﻿#region File Header
//-----------------------------------------------------------------------------
// Bricks' Bane
//
// Copyright (C) 2010 Julien Villers
// This program is distributed under the terms of the 
// GNU Lesser General Public License (LGPL).
// See Docs/lgpl.txt and Docs/gpl.txt
//-----------------------------------------------------------------------------
#endregion

using System;
using System.IO;
using BricksBane.Entities;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BricksBane.Building
{
    /// <summary>
    /// 'Level' factory.
    /// </summary> 
    class LevelBuilder
    {
        public LevelBuilder()
        {
        }

        public Wall Wall
        {
            get;
            private set;
        }

        public string LevelName
        {
            get;
            private set;
        }

        public void Load(string filename)
        {
            using (StreamReader reader = new StreamReader(filename))
            {
                Wall = new Wall();
                LoadSettings(reader);
                LoadBricks(reader);
            }

            GC.Collect();
        }

        private void LoadSettings(StreamReader reader)
        {
            LevelName = reader.ReadLine();
            string levelColor = reader.ReadLine();
            string[] components = levelColor.Split(',');
            Wall.Color = new Color(byte.Parse(components[0]), byte.Parse(components[1]), byte.Parse(components[2]));        // TODO: helper function.

            string sizeLine = reader.ReadLine();
            int width = int.Parse(sizeLine.Substring(0, sizeLine.IndexOf(',')));
            int height = int.Parse(sizeLine.Substring(sizeLine.IndexOf(',') + 1));
            Wall.BrickSize = new Vector2(width, height);
        }

        private void LoadBricks(StreamReader reader)
        {
            BrickBuilder builder = new BrickBuilder();
            builder.Load(Wall, reader);
        }
    }
}
